package com.csii.adhoc.info.post.serviceImpl;

import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.adhoc.info.post.PostPo;
import com.csii.adhoc.info.post.mapper.PostMapper;
import com.csii.adhoc.info.post.service.PostService;
//import com.csii.adhoc.info.user.service.UserService;
import com.csii.adhoc.info.user.service.UserService;
import com.csii.adhoc.util.DateUtils;
import com.csii.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 职务表接口实现类
 * @author: panChuanJiang
 * @create: 2021-01-27 11:23
 */
@Slf4j
@Service("postService")
public class PostServiceImpl implements PostService {
    @Resource
    private PostMapper postMapper;
    @Autowired
    private UidGenerator uidGenerator;
    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public void postMaintain(PostPo postPo, String optMode) {
        switch (optMode) {
            case CommonConstant.OPT_MODE_ADD:
                postPo.setCreatedBy(postPo.getUpdatedBy())
                        .setCreatedDt(postPo.getUpdatedDt());
                insertOnePost(postPo);
                break;
            case CommonConstant.OPT_MODE_DELETE:
                deletePostById(postPo.getPostId());
                break;
            case CommonConstant.OPT_MODE_UPDATE:
                updatePost(postPo);
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("optMode值不合法:" + optMode);
        }
    }

    @Transactional
    public void insertOnePost(PostPo postPo){
        String nowTime = DateUtils.getDateString("yyyyMMddHHmmss");
        postPo.setCreatedDt(nowTime)
                .setUpdatedDt(nowTime)
                .setUpdatedBy(postPo.getCreatedBy())
                .setPostId(uidGenerator.getUID() + "");
        postMapper.insertOnePost(postPo);
    }

    @Transactional
    public void deletePostById(String postId){
        postMapper.deletePostById(postId);
        userService.updateUserClearPostId(postId);
    }

    @Transactional
    @Override
    public void deletePostByDepartId(String departID){
        postMapper.deletePostByDepartId(departID);

    }

    @Transactional
    public void updatePost(PostPo postPo){
        postMapper.updatePostPo(postPo);
    }

    @Override
    public List<PostPo> selectListPost(PostPo postPo) {
        return postMapper.selectListPostByDepartId(postPo);
    }
}
